import { Controller, Get, Inject } from '@nestjs/common';
import { ApiTags, ApiOperation, ApiResponse } from '@nestjs/swagger';
import { HttpService } from './app.service';

@ApiTags('用户API')
@Controller('user')
export class UserApiController {
  private readonly apiConfig: any;

  constructor(
    private readonly httpService: HttpService<any>,
    @Inject('API_CONFIG') apiConfig: any
  ) {
    this.apiConfig = apiConfig;
  }

  /**
   * 调用外部API获取用户列表
   * @returns 用户列表数据
   */
  @Get('list')
  @ApiOperation({ summary: '获取用户列表', description: '调用外部API获取用户列表' })
  @ApiResponse({ status: 200, description: '成功获取用户列表' })
  @ApiResponse({ status: 500, description: '服务器内部错误' })
  async getUserList() {
    const { baseUrl, endpoints, token } = this.apiConfig;
    const params = {
      page: 1,
      pageSize: 10,
    };
    const url = `${baseUrl}${endpoints.userList}?page=${params.page}&pageSize=${params.pageSize}`;
    const headers = {
      'Token': token,
      'Content-Type': 'application/json',
    };

    try {
      const response = await this.httpService.get(url, headers);
      return response;
    } catch (error) {
      throw new Error(`调用API失败: ${error.message}`);
    }
  }
}